import useRsaStore from "@/store/modules/RsaStore.js";
import {encryptByPublicKey} from "@/utils/RsaUtils.js";
import {randomString} from "@/utils/RandomUtils.js";
import {decryptByDes, DES_KEY_LEN} from "@/utils/DesUtils.js";
import {strIsNull} from "@/utils/StringUtils.js";

/**
 * rsa 公钥加密
 *
 * @param data
 * @returns {string}
 */
export const rsaPubEncrypt = (data) => {
    return strIsNull(data) ? '' : encryptByPublicKey(useRsaStore().publicKey, data)
}

/**
 * 生成 DES 密钥
 * @returns {string}
 */
export const genDesKey = () => {
    return randomString(DES_KEY_LEN)
}

/**
 * 对 rsa 中的 data 进行 des 解密
 *
 * @param res
 * @param desKey
 * @returns {*}
 */
export const desDecrypt = (res, desKey) => {
    res.data = JSON.parse(decryptByDes(res.data, desKey))
    return res
}